package com.wuliaogame.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wuliaogame.auto.JSONAuthentication;
import com.wuliaogame.entity.PApply;
import com.wuliaogame.entity.PClub;
import com.wuliaogame.entity.PTuihui;
import com.wuliaogame.entity.PUser;
import com.wuliaogame.mapper.PApplyMapper;
import com.wuliaogame.mapper.PUserMapper;
import com.wuliaogame.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuliaogame
 * @since 2022-04-20
 */
@RestController
@RequestMapping("/p-apply")
public class PApplyController extends JSONAuthentication {
    @Autowired
    PApplyMapper pApplyMapper;
    @Autowired
    PUserMapper pUserMapper;
    @ApiOperation(value = "添加进入俱乐部申请")
    @PostMapping("/add")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("userId") int userId,@RequestParam("clName")String clName) throws Exception {
        PUser pUser = pUserMapper.selectById(userId);
        if(pUser.getClub()!=null){
            Result error = Result.error().message("已有俱乐部，不可申请");
            this.WriteJSON(request, response, error);
        }else {
          QueryWrapper ww=new QueryWrapper();
          ww.eq("user_id",userId);
          ww.eq("status","待审核");
          if(pApplyMapper.selectOne(ww)!=null){
              Result error = Result.error().message("已有申请，不可重复申请");
              this.WriteJSON(request, response, error);
          }else{
              PApply pApply=new PApply();
              pApply.setUserId(userId);
              pApply.setClId(clName);
              Date date=new Date();
              Timestamp timestamp=new Timestamp(date.getTime());
              pApply.setCreatTime(timestamp);
              int i = pApplyMapper.insert(pApply);
              if (i > 0) {
                  Result ok = Result.ok().message("申请成功");
                  this.WriteJSON(request, response, ok);
              } else {
                  Result error = Result.error().message("申请失败");
                  this.WriteJSON(request, response, error);
              }
          }
        }

    }
    @ApiOperation(value = "删除申请")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {
        int i = pApplyMapper.deleteById(id);
        if (i > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改申请状态(通过/未通过)")
    @PostMapping("/upd")
    public void upd(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id,@RequestParam("status")String status) throws Exception {
        PApply pApply = pApplyMapper.selectById(id);
        pApply.setStatus(status);
        PUser pUser=pUserMapper.selectById(pApply.getUserId());
        pUser.setClub(pApply.getClId());
        if (pApplyMapper.updateById(pApply) > 0&&pUserMapper.updateById(pUser)>0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }


    @ApiOperation(value = "查找全部申请")
    @PostMapping("/find")
    public void findBynamea(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current) throws Exception {

        Page<PApply> page = new Page<>(current, size);
        Page<PApply> pages = pApplyMapper.selectPage(page, null);
        List<PApply> list = pages.getRecords();
        for (PApply a: list){
        a.setPUser(pUserMapper.selectById(a.getUserId()));
        }
        int total = pApplyMapper.selectCount(null);
        if (list.size() > 0) {
            Result ok = Result.ok().data("apply", list).data("total", total);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("没有申请");
        this.WriteJSON(request, response, error);
    }

    @ApiOperation(value = "查找全部待审核申请")
    @PostMapping("/findNoSh")
    public void findNoSh(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("status","待审核");
        Page<PApply> page = new Page<>(current, size);
        Page<PApply> pages = pApplyMapper.selectPage(page, null);
        List<PApply> list = pages.getRecords();
        for (PApply a: list){
            a.setPUser(pUserMapper.selectById(a.getUserId()));
        }
        int total = pApplyMapper.selectCount(null);
        if (list.size() > 0) {
            Result ok = Result.ok().data("apply", list).data("total", total);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("没有申请");
        this.WriteJSON(request, response, error);
    }
    @ApiOperation(value = "查找全部通过的申请")
    @PostMapping("/findTg")
    public void findTg(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("status","通过");
        Page<PApply> page = new Page<>(current, size);
        Page<PApply> pages = pApplyMapper.selectPage(page, null);
        List<PApply> list = pages.getRecords();
        for (PApply a: list){
            a.setPUser(pUserMapper.selectById(a.getUserId()));
        }
        int total = pApplyMapper.selectCount(null);

        if (list.size() > 0) {
            Result ok = Result.ok().data("apply", list).data("total", total);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("没有申请");
        this.WriteJSON(request, response, error);
    }
    @ApiOperation(value = "查找全部未通过的申请")
    @PostMapping("/findNoTg")
    public void findNoTg(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("status","未通过");
        Page<PApply> page = new Page<>(current, size);
        Page<PApply> pages = pApplyMapper.selectPage(page, null);
        List<PApply> list = pages.getRecords();
        for (PApply a: list){
            a.setPUser(pUserMapper.selectById(a.getUserId()));
        }
        int total = pApplyMapper.selectCount(null);
        if (list.size() > 0) {
            Result ok = Result.ok().data("apply", list).data("total", total);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("没有申请");
        this.WriteJSON(request, response, error);
    }
    @ApiOperation(value = "查找申请通过id")
    @PostMapping("/findByid")
    public void findByid(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {
        PApply pTuihui = pApplyMapper.selectById(id);
        pTuihui.setPUser(pUserMapper.selectById(pTuihui.getUserId()));
        if (pTuihui!=null) {
            Result ok = Result.ok().data("apply", pTuihui);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("没有申请");
        this.WriteJSON(request, response, error);
    }


}

